using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ObjectLib;

namespace ObjectParser
{
	/// <summary>
	/// Summary description for PickObjectDialog.
	/// </summary>
	public class PickObjectDialog : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button butPick;
		private System.Windows.Forms.ListBox listBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public PolygonF SelectedPoly;
		public PolygonFlist PolyList;

		public PickObjectDialog(PolygonFlist polys)
		{
			InitializeComponent();
			
			if(polys != null)
			{
				PolyList = polys;
				SelectedPoly = polys[0];

				for(int i=0; i<PolyList.Count; i++)
					listBox1.Items.Add( PolyList[i].Owner.Name
						+ " " + Convert.ToString(PolyList[i].Owner.Polygons.IndexOf(PolyList[i])) );
				listBox1.SelectedIndex=0;
				listBox1.Focus();
			}
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.butPick = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// butPick
			// 
			this.butPick.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.butPick.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.butPick.Location = new System.Drawing.Point(8, 184);
			this.butPick.Name = "butPick";
			this.butPick.Size = new System.Drawing.Size(208, 23);
			this.butPick.TabIndex = 0;
			this.butPick.Text = "Pick";
			this.butPick.Click += new System.EventHandler(this.butPick_Click);
			// 
			// listBox1
			// 
			this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.listBox1.Location = new System.Drawing.Point(8, 8);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(208, 171);
			this.listBox1.TabIndex = 1;
			this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
			this.listBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyUp);
			// 
			// PickObjectDialog
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(224, 214);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.butPick);
			this.Name = "PickObjectDialog";
			this.Text = "Pick a polygon";
			this.ResumeLayout(false);

		}
		#endregion

		private void butPick_Click(object sender, System.EventArgs e)
		{
			Pick();
		}

		private void listBox1_DoubleClick(object sender, System.EventArgs e)
		{
			Pick();
		}

		private void listBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter) Pick();
		}

		private void Pick()
		{
			if(listBox1.SelectedIndex >= PolyList.Count)
				SelectedPoly = PolyList[PolyList.Count-1];
			else SelectedPoly = PolyList[listBox1.SelectedIndex];
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
	}
}
